//
//  QYMoreCommentsViewController.m
//  QingYue
//
//  Created by Zheng on 2017/6/16.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYMoreCommentsViewController.h"
#import "QYMoreCommentsCell.h"
#import "QYMoreCommentsModel.h"

static NSString *kQYMoreCommentsCell_ID = @"kQYMoreCommentsCell_ID";

@interface QYMoreCommentsViewController ()

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation QYMoreCommentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"评论";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"QYMoreCommentsCell" bundle:nil] forCellReuseIdentifier:kQYMoreCommentsCell_ID];
    
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self loadData];
    
}

- (void)loadData {
    [SVProgressHUD show];
    NSDictionary *param = @{@"username":[kUserDefaults objectForKey:@"username"]};
    [[YCRequestDataService sharedManager] Post:@"GetComment" parameters:param success:^(id response) {
        _dataArr = [NSMutableArray new];
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            NSArray *list = response[@"followsList"];
            [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                QYMoreCommentsModel *model = [[QYMoreCommentsModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
            }];
            [SVProgressHUD dismissWithDelay:.35];
            [self.tableView reloadData];
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

- (void)deleteRequest:(NSNumber *)isbn {
    [SVProgressHUD show];
    NSDictionary *param = @{@"username":[kUserDefaults objectForKey:@"username"],
                            @"Isbn":isbn};
    [[YCRequestDataService sharedManager] Post:@"DelComment" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            [self loadData];
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QYMoreCommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:kQYMoreCommentsCell_ID forIndexPath:indexPath];
    
    cell.deleteBlock = ^(NSString *isbn) {
        [self deleteRequest:isbn];
    };
    
    QYMoreCommentsModel *model = _dataArr[indexPath.row];
    cell.model = model;
    
    return cell;
}

#pragma mark - table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}


@end
